package controller

import (
	"code-sandbox/src/main/model/entity"
	"code-sandbox/src/main/service"
	"code-sandbox/src/main/utils"
	"github.com/gin-gonic/gin"
	"log"
)

// 这里为了方便直接硬编码进去ak
var (
	ak = "123"
	sk = "456"
)
var SandBoxService = service.NewSandBoxService()

func RunGoCode(c *gin.Context) {
	var questionSubmit entity.QuestionSubmit
	err := c.ShouldBindBodyWithJSON(&questionSubmit)
	if err != nil {
		return
	}
	// 获取客户端生成的签名
	getauth := c.GetHeader("auth")
	getsk := c.GetHeader("sk")
	// 验证签名
	signature := utils.Auth(getsk)
	// 如果客户端传输的签名和服务端传输的签名不一致，返回错误
	if getauth != signature {
		c.JSON(403, gin.H{"error": "签名错误"})
		return
	}
	// 签名正确，执行下面的逻辑,调用业务层
	response, err := SandBoxService.RunCode(&questionSubmit)
	if err != nil {
		log.Println(err)
		return
	}
	c.JSON(200, response)
}
